package com.ytt.core.api.controller.college;

import com.ytt.core.model.authority.AuthorityDto;
import com.ytt.core.model.college.CollegeDto;
import com.ytt.core.model.college.CollegeEditDto;
import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.service.service.ProposalQueryParam;
import com.ytt.core.service.service.college.CollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

@Api(tags = {"学院管理"})
@RestController
@RequestMapping("/api/college")
@Validated
public class CollegeController {

    private final CollegeService collegeService;

    @Autowired
    public CollegeController(CollegeService collegeService) {
        this.collegeService = collegeService;
    }


    @ApiOperation(value = "根据ID查找学院")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('COLLEGE_RETRIEVE')")
    public CollegeDto findById(
            @ApiParam(value = "学院ID", required = true) @PathVariable String id) {
        return collegeService.findById(id);
    }

    @ApiOperation(value = "分页查找学院")
    @GetMapping(value = "")
    @PreAuthorize("hasAnyAuthority('COLLEGE_RETRIEVE')")
    public PageDataDTO<CollegeDto> findOnePage(
            @ApiParam(value = "页号，从0开始", defaultValue = "0") @RequestParam(value = "page", required = false) @Min(0) Integer page,
            @ApiParam(value = "每页纪录条数", defaultValue = "20") @RequestParam(value = "size", required = false) @Min(1) @Max(100) Integer size,
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(value = "学院名称") @RequestParam(value = "name", required = false) String name) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .build();
        return collegeService.findOnePage(page, size, sort, param);
    }

    @ApiOperation(value = "查找所有学院")
    @GetMapping(value = "/all")
    @PreAuthorize("hasAnyAuthority('COLLEGE_RETRIEVE')")
    public List<CollegeDto> findAll(
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", defaultValue = "id", required = false) String sort,
            @ApiParam(value = "学院名称") @RequestParam(value = "name", required = false) String name) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .build();
        return collegeService.findAll(sort,param);
    }



    @ApiOperation(value = "创建学院")
    @PostMapping(value = "")
    @PreAuthorize("hasAnyAuthority('COLLEGE_CREATE')")
    public CollegeDto create(@ApiParam(value = "学院信息", required = true) @RequestBody @Validated CollegeEditDto collegeEditDto) {
        return collegeService.create(collegeEditDto);
    }

    @ApiOperation(value = "更新学院")
    @PutMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('COLLEGE_UPDATE')")
    public CollegeDto update(@ApiParam(value = "学院ID", required = true) @PathVariable String id,
                               @ApiParam(value = "学院信息", required = true) @RequestBody @Validated CollegeEditDto collegeEditDto) {
        return collegeService.update(id, collegeEditDto);
    }

    @ApiOperation(value = "删除学院")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('COLLEGE_DELETE')")
    public void delete(@ApiParam(value = "学院ID", required = true) @PathVariable String id) {
        collegeService.delete(id);
    }
}
